package Page.BookBorrowingRecordPage;

import Navigation.NavigationController;
import DataClass.BookBorrowingRequestRecord;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @since 2024-06-02
 * @author yooo_fan
 * 图书借阅页面viewModel
 */
public class BookBorrowingRecordViewModel {
    ListProperty<BookBorrowingRequestRecord> dataList = new SimpleListProperty<>(FXCollections.observableArrayList());
    // 列表副本
    private List<BookBorrowingRequestRecord> bookBorrowingRecordsCopy = new ArrayList<BookBorrowingRequestRecord>();

    // 用户查询书籍信息
    private final StringProperty inputBookName = new SimpleStringProperty();
    private final StringProperty inputPeopleName = new SimpleStringProperty();

    public ListProperty<BookBorrowingRequestRecord> getDataListProperty() {
        return dataList;
    }

    public ObservableList<BookBorrowingRequestRecord> getDataList() {
        return dataList.get();
    }

    public void setDataList(ObservableList<BookBorrowingRequestRecord> dataList) {
        // 保存列表副本
        this.dataList.set(dataList);
    }

    public void setDataList(List<BookBorrowingRequestRecord> dataList) {

        this.bookBorrowingRecordsCopy = dataList;
        setDataList(FXCollections.observableArrayList(dataList));
    }

    public void backButtonClicked(){
        NavigationController.back();
    }

    public void addButtonClicked() {
        System.out.println("正在启动登记页面");
        try{
            FXMLLoader loader = new FXMLLoader(getClass().getResource("AddRecordPage.fxml"));
            Parent root = loader.load();

            Stage stage = new Stage();
            stage.initModality(Modality.WINDOW_MODAL);
            stage.initOwner(NavigationController.getLastScene().getWindow());
            stage.setTitle("书籍借阅登记");
            stage.setScene(new Scene(root));
            stage.showAndWait();
            System.out.println("启动完毕");
        }catch (IOException e){
            System.err.println("AddRecord loading error");
        }
    }

    public StringProperty getInputBookName(){
        return inputBookName;
    }
    public void updateInputBookName(String string)
    {
        inputBookName.set(string);
    }

    public StringProperty getInputPeopleName(){
        return inputPeopleName;
    }
    public void updateInputPeopleName(String string)
    {
        inputPeopleName.set(string);
    }

    // 根据用户输入过滤
    public void clickFindButton(){
        String targetBookName = inputBookName.get();
        String targetPeopleName = inputPeopleName.get();

        if(targetBookName!=null && targetPeopleName!=null && !targetPeopleName.isEmpty() && !targetBookName.isEmpty()){
            FilteredList<BookBorrowingRequestRecord> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(bookBorrowingRecordsCopy),
                    p -> (p.getRbName().equals(targetBookName) && p.getRPeople().equals(targetPeopleName))
            );
            dataList.setAll(filteredData);
            return;
        }
        if (targetBookName != null && !targetBookName.isEmpty()) {
            FilteredList<BookBorrowingRequestRecord> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(bookBorrowingRecordsCopy),
                    p -> p.getRbName().equals(targetBookName)
            );
            dataList.setAll(filteredData);
            return;
        }
        if(targetPeopleName != null && !targetPeopleName.isEmpty()) {
            FilteredList<BookBorrowingRequestRecord> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(bookBorrowingRecordsCopy),
                    p -> p.getRPeople().equals(targetPeopleName)
            );
            dataList.setAll(filteredData);
        }
    }

    // 清空过滤
    public void clearFilter(){
        this.inputBookName.set(null);
        this.setDataList(bookBorrowingRecordsCopy);
    }

    // 刷新
    public void refresh(){
        this.inputBookName.set(null);
    }
}